package com.example.administrator.guidepro.mode;

import com.example.administrator.guidepro.bean.WetherResult;
import com.example.administrator.guidepro.iface.WetherInface;
import com.example.administrator.guidepro.listenr.WetherListener;
import com.example.administrator.guidepro.service.Wetherservice;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/3/21.
 */

public class WetherMode implements WetherInface {
    private static final String BASE_URL="http://www.weather.com.cn/data/";
    private Wetherservice service;
    private Retrofit retrofit;
    @Override
    public void getWether(String cityNumber, final WetherListener listener) {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Wetherservice.class);
        Call<WetherResult> call=service.getResult(cityNumber);
        call.enqueue(new Callback<WetherResult>() {
            @Override
            public void onResponse(Call<WetherResult> call, Response<WetherResult> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    listener.OnResponse(response.body().getWetherinfo());
                }
            }

            @Override
            public void onFailure(Call<WetherResult> call, Throwable t) {
                listener.OnFailure(t.toString());

            }
        });

    }
}
